package br.com.romerito.gui.logo;

import br.com.romerito.app.MidletApp;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

/**
 *
 * @author Romerito Campos
 */
public class Turtle extends Sprite {

    private int degree = 90;

    public Turtle(Image sprite) {

        super(sprite);
       
        defineReferencePixel(14, 14);
        setTransform(TRANS_NONE);
    }

    public Turtle(Image sprite, int frameWidth, int frameLenght) {
        super(sprite, frameWidth, frameLenght);
    }

    
    public int getDegree() {
        return degree;
    }

   
    public void setDegree(int degree) {
        this.degree = degree;
    }

    public void transform(String cmd, int grau) {

        int x = getX();
        int y = getY();

        int temp = -1;


        if (cmd.equals("pe ")) {
            temp = getDegree() + grau;

            if (temp > 360) {
                temp = temp % 360;
            }
        } else if (cmd.equals("pd ")) {
            temp = getDegree() - grau;

            if (temp < 0) {

                if (temp < -360) {
                    temp = temp % 360;
                    temp = 360 + temp;
                } else {
                    temp = 360 + temp;
                }
            }
        }

        System.out.println(temp);

        if (temp >= 0 && temp <= 90) {
            setTransform(TRANS_NONE);
            setFrame(temp);
            setDegree(temp);
        }

        if (temp > 90 && temp <= 180) {
            setTransform(TRANS_NONE);
            setFrame(temp - 90);
            setTransform(TRANS_ROT270);
            setDegree(temp);
        }

        if (temp > 180 && temp <= 270) {
            setTransform(TRANS_NONE);
            setFrame(temp - 180);
            setTransform(TRANS_MIRROR);
            setTransform(TRANS_ROT180);
            setDegree(temp);

        }

        if (temp > 270 && temp <= 360) {
            setTransform(TRANS_NONE);
            setFrame(temp - 270);
            setTransform(TRANS_ROT90);
            setDegree(temp);
        }

        setPosition(x, y);

    }

    public void move(int distance, String cmd, ViewWindow viewWindow) {

       
        double x = getNewX(distance);
        double y = getNewY(distance);

        int currentX = getX();
        int currentY = getY();

        Graphics g = viewWindow.getViewWindow().getGraphics();

        int center = this.getWidth() / 2;


        g.setColor(0, 0, 0);

        if (cmd.equals("pf ")) {

            if (getDegree() > 0 && getDegree() <= 90) {
                y = -1 * y;
               
            }

            if (getDegree() > 90 && getDegree() <= 180) {
                x = -1 * x;
                y = -1 * y;

                
            }

            if (getDegree() > 180 && getDegree() <= 270) {
                x = -1 * x;

                
            }

            setPosition((int) x + getX(), (int) y + getY());
        }

        if (cmd.equals("pt ")) {

            if (getDegree() >= 0 && getDegree() <= 90) {                
                x = -1 * x;               
                
            }

            if (getDegree() > 180 && getDegree() <= 270) {
                y = -1 * y;

                
            }

            if (getDegree() > 270 && getDegree() <= 360) {
                x = -1 * x;
                y = -1 * y;                
               
            }

            setPosition((int) x + getX(), (int) y + getY());

        }

        g.drawLine(currentX + center, currentY + center, this.getX() + center, this.getY() + center);

        if (getDegree() % 90 == 0) {
            int yLimit = MidletApp.app.getScreenLogo().getHeight() - 20;
            int xLimit = MidletApp.app.getScreenLogo().getWidth();

            int posY = 0;
            int posX = 0;

            if (this.getY() < 0) {
                posY = yLimit + getY();
                g.drawLine(getX() + center, yLimit + getY() + center, getX() + center, yLimit + center);
                setPosition(getX(), posY);                
            }

            if (this.getY() > yLimit) {
                posY = getY() - yLimit;
                g.drawLine(getX() + center, 0, getX() + center, posY + center);
                g.translate(25, 0);
                setPosition(getX(), getY() - yLimit);                
            }

            if (this.getX() < 0) {
                posX = xLimit + getX();
                g.drawLine(posX + center, getY() + center, xLimit + center, getY() + center);
                setPosition(posX, getY());                
            }

            if (this.getX() > xLimit) {
                posX = getX() - xLimit;
                g.drawLine(0, getY() + center, posX + center, getY() + center);
                setPosition(posX, getY());                
            }
        }

    }

    public double getNewX(int distance) {
        return Math.abs(distance * -1 * Math.cos((getDegree() * Math.PI) / 180));
    }

    public double getNewY(int distance) {
        return Math.abs(distance * Math.sin((getDegree() * Math.PI) / 180));
    }
    
}
